import QtQuick 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"


Item {
  id: mainCpt //gere l accès à une appli
  property string appName //le nom de l appli, qui sera aussi le nom de l accès dbus dans le rootContexte
  property string specialcmdLauncher //si pour lancer l appli il faut ua autre commande
  //property bool autoLaunch : false
  property bool running : false //indique que l appli est lancée
  property alias dbusServ : dbusSer
  property alias shellObj : shellLauncher
  signal showed()
  signal hided()
  signal onHome()
  
  function init(conf) {
    var c =conf.value('cmd');
    if(c)
      specialcmdLauncher=c;
    
    var launchAuto = conf.isValue('autoLaunch');
    console.log('autoLaunch:' + launchAuto);
    if(launchAuto){
      //console.log('autoLaunching==>');
      launch();
    }
    console.log('init from conf OK');
  }
  
  DbusService {
      id : dbusSer
      path:'/generic/'+mainCpt.appName
      service:'org.kdeadmouse.apps.'+mainCpt.appName
      registerName : mainCpt.appName
      
      onInitialized :{
	console.log('init connecting');
	dbus.showed.connect(mainCpt.showed);
	dbus.hided.connect(mainCpt.hided);
	dbus.onHome.connect(mainCpt.onHome);
      }
	
      Component.onCompleted: {
	checkServiceExists();
      }
  }
    
  ShellInvoker {
      id:shellLauncher
      waitFor:'start'
      onOutputLineChanged : {
	  var out = shellLauncher.outputLine;
	  console.log('**' +mainCpt.appName+ '**'  + out);
      }
      
      onLaunched : {
	//console.log("===================>launched:");
	mainCpt.running=true;
      }
      
      onFinished : {
	//console.log("===================>finished");
	mainCpt.running=false;
      }
  }
  
  function launch() {
      if(!mainCpt.running){
	if(mainCpt.specialcmdLauncher)
	  shellLauncher.shellAction(mainCpt.specialcmdLauncher,[]);
	else
	  shellLauncher.shellAction('src/QTproject/desktopApps/QmlPlayer-build-desktop/QmlPlayer',[mainCpt.appName]);
      }
  }
  
}
    
   